//
//  MapViewController.m
//  FlowingData
//
//  Created by NIMESH DESAI on 10/25/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MapViewController.h"

@implementation MapViewController

@synthesize locationManager;

#pragma mark -
#pragma mark initWithNibName

 // The designated initializer.  Override if you create the controller programmatically 
 // and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
	{
        // Custom initialization
    }
    return self;
}

#pragma mark -
#pragma mark viewDidLoad

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
	map = [[MKMapView alloc] initWithFrame:self.view.bounds];
	[self.view insertSubview:map atIndex:0];
	map.showsUserLocation = YES;
	map.mapType = MKMapTypeHybrid;
	
	self.locationManager = [[CLLocationManager alloc] init];
	self.locationManager.delegate = self;
	self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
	[self.locationManager startUpdatingLocation];
	latitude = [[NSString alloc] init];
	longitude = [[NSString alloc] init];
}

#pragma mark -
#pragma mark NSURL Connection Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *)response;
	NSLog(@"didReceiveResponse response = %@", httpResponse);
	statusCode = [httpResponse statusCode];
	NSLog(@"headers = %@", [httpResponse allHeaderFields]);
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
	NSLog(@"didReceiveData - data = %@", data);
	NSString* string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"didReceiveData - data to string = %@", string);
	if( statusCode == 200 )
	{
		if( [string isEqualToString:@"Error - Require subject and predicate."] == YES )
		{
				NSLog(@"Enter Username");
		}			
	}
	[string release];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	NSLog(@"connection did finish loading");
	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	[connection release];
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	[connection release];
	NSLog(@"didFailWithError error = %@", error);
}

#pragma mark -
#pragma mark PostData

- (void) _postData
{
	appDelegate =		[[UIApplication sharedApplication] delegate];
	NSString* user =	[[NSString alloc] initWithFormat:@"%@", appDelegate.userName];		
	NSString* pass =	[[NSString alloc] initWithFormat:@"%@", appDelegate.passWord];
	NSString* temp_Location = [[NSString alloc] initWithFormat:@"%@,%@", latitude, longitude];	
	urlString =			[[NSString alloc] initWithFormat:
						@"http://www.ist.rit.edu/~acjvks/rmail/admin/add_triple.php?s=%@&p=location&o=%@&password=%@",
						user, temp_Location, pass];
	NSLog(@"%@", urlString);		
	NSURL* url = [NSURL URLWithString:urlString];
	[urlString release];
	NSURLRequest* request =	[NSURLRequest requestWithURL:url
							cachePolicy:NSURLRequestUseProtocolCachePolicy
							timeoutInterval:10.0];
	
	NSURLConnection* connection =	[[NSURLConnection alloc] initWithRequest:request delegate:self];
	if( connection )
		[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
	
	[user release];
	[pass release];
	[temp_Location release];
}

#pragma mark -
#pragma mark locationManager

- (void)locationManager:(CLLocationManager *)manager
						didUpdateToLocation:(CLLocation *)newLocation
						fromLocation:(CLLocation *)oldLocation
{
	latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
	longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
	NSLog(@"latitude = %@", latitude);
	NSLog(@"longitude = %@", longitude);
	[self _postData];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

#pragma mark -
#pragma mark memory and unload methods

- (void)didReceiveMemoryWarning
{
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	// Release any cached data, images, etc that aren't in use
}

- (void)viewDidUnload
{
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc
{
	[latitude release];
	[longitude release];
	[locationManager release];
	[map release];
    [super dealloc];
}

#pragma mark -

@end
